<?php
$type = $_GET['type'];
$res = ['status'=>0,'data'=>[],'message'=>''];
if ($type == 'login') {
    $data['email'] = $email = $_POST['email'];
    $data['password'] = $password = $_POST['password'];
    $data['captcha'] = $captcha = $_POST['captcha'];
    $data['is_auto'] = $is_auto = !empty($_POST['is_auto']) && $_POST['is_auto'] === 'on' ?true:false;
    if ($email == 'zhuhui@yidian-mall.com' && $password == 'QWEasd123' && $captcha == '12345678') {
        $res['status'] = 1;
        $res['message'] = '登录成功';
    }else{
        $res['message'] = '账户或密码错误';
    }
}

if ($type == 'register') {
    $res['status'] = 1;
    $res['message'] = '注册完成';
}

if ($type == 'forget') {
    $res['status'] = 1;
    $res['message'] = '密码已经重置成功';
}

if ($type == 'captcha') {
    $validateboxText = $_POST['validateboxText'];
    $res['status'] = 1;
    $res['message'] = $validateboxText."验证已经发送，请在有效期内使用,[<a href='http://www.baidu.com'>链接</a>]";
}

if ($type == 'test') {
    sleep(3);
    $res['status'] = 1;
    $res['data']['post'] = $_POST;
    $res['data']['file'] = $_FILES;
    $res['data']['get'] = $_GET;
    $res['message'] = '获取数据成功';
}

if ($type == 'table11') {
    $res['status'] = 1;
    $res['message'] = '获取数据成功';
}

if ($type == 'select') {
    $res = [
        ["text"=>"ar","desc"=>"Arabic",],
        ["text"=>"bg","desc"=>"Bulgarian",],
        ["text"=>"ca","desc"=>"Catalan",],
        // ["text"=>"zh-cht","desc"=>"ChineseTraditional","selected"=>true,],
        ["text"=>"cs","desc"=>"Czech",],
        ["text"=>"da","desc"=>"Danish",],
        ["text"=>"nl","desc"=>"Dutch",],
        ["text"=>"en","desc"=>"English",],
        ["text"=>"et","desc"=>"Estonian",],
        ["text"=>"fi","desc"=>"Finnish",],
        ["text"=>"fr","desc"=>"French",],
        ["text"=>"de","desc"=>"German",],
        ["text"=>"el","desc"=>"Greek",],
        ["text"=>"ht","desc"=>"HaitianCreole",],
        ["text"=>"he","desc"=>"Hebrew",],
        ["text"=>"hi","desc"=>"Hindi",],
        ["text"=>"mww","desc"=>"Hmong",],
        ["text"=>"hu","desc"=>"Hungarian",],
        ["text"=>"id","desc"=>"Indonesian",],
        ["text"=>"it","desc"=>"Italian",],
        ["text"=>"ja","desc"=>"Japanese",],
        ["text"=>"ko","desc"=>"Korean",],
        ["text"=>"lv","desc"=>"Latvian",],
        ["text"=>"lt","desc"=>"Lithuanian",],
        ["text"=>"no","desc"=>"Norwegian",],
        ["text"=>"fa","desc"=>"Persian",],
        ["text"=>"pl","desc"=>"Polish",],
        ["text"=>"pt","desc"=>"Portuguese",],
        ["text"=>"ro","desc"=>"Romanian",],
        ["text"=>"ru","desc"=>"Russian",],
        ["text"=>"sk","desc"=>"Slovak",],
        ["text"=>"sl","desc"=>"Slovenian",],
        ["text"=>"es","desc"=>"Spanish",],
        ["text"=>"sv","desc"=>"Swedish",],
        ["text"=>"th","desc"=>"Thai",],
        ["text"=>"tr","desc"=>"Turkish",],
        ["text"=>"uk","desc"=>"Ukrainian",],
        ["text"=>"vi","desc"=>"Vietnamese",],
    ];
}
if ($type == 'table') {
    sleep(1);
    $file_path = "./table.php";
    $page = isset($_POST['page']) ? $_POST['page']-1:0;
    $prePage = isset($_POST['rows']) ? $_POST['rows']:10;
    $data = [];
    $res['total'] = 0;
    $res['rows'] = $data;
    if(file_exists($file_path)){
        $data = require($file_path);
        $res['total'] = count($data);
    }
    foreach ($_POST as $key => $value) {
        $res['data'][$key] = $value;
    }
    if ($data) {
        $res['rows'] = array_chunk($data,$prePage)[$page];
    }
    $res['status'] = 1;
    $res['message'] = '操作成功';
}

if ($type == 'batch') {
    sleep(1);
    $res['status'] = 1;
    $res['message'] = '操作成功';
    $res['data'] = $_POST;
}

if ($type == 'row') {
    sleep(1);
    $res['status'] = 1;
    $res['message'] = '操作成功';
    $res['data'] = $_POST;
}

if ($type == 'export') {
    $res['status'] = 1;
    $res['message'] = '操作成功';
    $res['data'] = $_POST;
}

if ($type == 'filelist') {
    $basedir = './files/';
    if (isset($_GET['m'])) {
        if ($_GET['m'] == 'd') {
            $res['status'] = 1;
            $path = $_POST['filePath'];
            $path = str_replace('/data/files/',$basedir, $path);
            if (file_exists($path)) {
                if (unlink($path)){
                    $res['message'] = '文件已经删除成功';
                }else{
                    $res['status'] = 0;
                    $res['message'] = '文件删除失败';
                }
            }else{
                $res['message'] = '文件不存在';
            }
        }elseif ($_GET['m'] == 'u'){
            $res['status'] = 1;
            $res['message'] = '操作成功';

            $files = [];
            foreach ($_FILES as $temp) {
                if ($temp['error'] > 0) {
                    continue;
                }
                $progress = 0;
                $fileStatus = 0;
                $path = $basedir.$temp['name'];
                if (file_exists($path)) {
                    // 文件已存在
                    $progress = 100;
                    $fileStatus = 1;
                }else{
                    $fileStatus = -1;
                    if (move_uploaded_file($temp['tmp_name'],$path)) {
                        $progress = 100;
                        $fileStatus = 1;
                    }
                }
                $files = [
                    'fileName'=>$temp['name'],
                    'fileSize'=>$temp['size'],
                    'fileType'=>$temp['type'],
                    'filePath'=>str_replace($basedir,'', '/data/files/'.$path),
                    'progress'=>$progress,
                    'fileStatus'=>$fileStatus,
                ];
            }
            $res['fileInfo'] = $files;
            $res['filePath'] = $files['filePath'];
        }else{
            $res['status'] = 0;
            $res['message'] = '操作失败';
        }
    }else{
        $scfiles = scandir($basedir);
        $files = [];

        // pathinfo($path, PATHINFO_EXTENSION);// 扩展名

        // 获取文件类型
        // $finfo = finfo_open(FILEINFO_MIME_TYPE);
        // $mime = finfo_file($finfo, $path);

        foreach ($scfiles as $file) {
            if ($file == '.' || $file == '..' || $file == '.gitignore') {
                continue;
            }
            $path = $basedir.$file;
            $files[] = [
                'fileName'=>$file,
                'fileSize'=>filesize($path),
                'fileType'=>pathinfo($path, PATHINFO_EXTENSION),
                'filePath'=>str_replace($basedir,'', '/data/files/'.$basedir.$file),
                'progress'=>100,
                'fileStatus'=>1,
            ];
        }
        $res['total'] = count($files);
        $res['rows'] = $files;
        $res['status'] = 1;
        $res['message'] = '操作成功';
    }
}

if ($type == 'echart') {
    sleep(1);
    $res['option'] = [
        'title'=>[
            'text'=>'异步数据加载示例',
            'subtext'=>'纯属虚构',
        ],
        'toolbox'=>[
            'feature'=>['restore'=>['show'=>false]],
        ],
        'visualMap'=>[
            'show'=>false,
            'min'=>0,
            'max'=>1000,
        ],
        'calendar'=>['range'=>'2017'],
        'series'=>[
            'type'=>'heatmap',
            'coordinateSystem'=>'calendar',
            'data'=>[
                ['2017-01-02', 900],
                ['2017-01-02', 877],
                ['2017-01-02', 699],
            ],
        ],
    ];
    $res['status'] = 1;
    $res['message'] = '操作成功';
}
echo json_encode($res);


